package UPPM;

import java.util.List;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.pagefactory.ElementLocator;
import org.openqa.selenium.support.ui.Select;


public class TestBrowse {
	WebDriver driver = new FirefoxDriver(); 

	@Before
	public void setUp() throws Exception {
		driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		driver.get("http://test.unippmtest.co.uk");
		}

//             INITIAL STATE TESTS	
//	
	@Test
	public void testBrowseTabUiElementsNotLoggedIn() throws Exception {
		
		Utils.waitForElement(driver, By.linkText("Browse Music"));
		Thread.sleep(3000);
		driver.findElement(By.linkText("Browse Music")).click();
//		System.out.println(browse_tab.getText());
//		Assert.assertEquals("Browse Music", browse_tab.getText());
//		Thread.sleep(3000);
//		browse_tab.click();
		
//		Actions builder = new Actions(driver);
//		builder.click(browse_tab).perform();
//		builder.doubleClick(browse_tab).perform();
//		builder.build().perform();
		
		
		
		System.out.println("TestBrowse>testBrowseTabUiElementsNotLoggedIn");	
		
		Utils.waitForElement(driver, By.id("labels_box"));
		Utils.isElementPresent(driver, By.id("labels_box"));
		Utils.isElementPresent(driver, By.id("expand_labels"));
		WebElement libraries = driver.findElement(By.linkText("Label"));
		Assert.assertEquals("Label", libraries.getText());	
		WebElement playlists = driver.findElement(By.linkText("Playlists"));
		Assert.assertEquals("Playlists", playlists.getText());	
		
		WebElement album_view = driver.findElement(By.id("btn_trackview"));
		Assert.assertEquals("Tile View", album_view.getText());	
		Assert.assertEquals(true, album_view.isEnabled());	
		Utils.isElementPresent(driver, By.id("btn_cdview"));
		Utils.isElementPresent(driver, By.id("sort"));
		Utils.isElementPresent(driver, By.id("results_num"));
		Utils.isElementPresent(driver, By.id("browse_list_acc"));
		
		WebElement list_view = driver.findElement(By.id("btn_cdview"));
		Assert.assertEquals("List View", list_view.getText());	
		list_view.click();
		Utils.waitForElement(driver, By.xpath("//*[@id='rowRel']/div[4]/div[1]"));
		//check header
		WebElement album_no = driver.findElement(By.xpath("//*[@id='rowRel']/div[4]/div[1]"));
		Assert.assertEquals("CD #", album_no.getText());
		WebElement title = driver.findElement(By.xpath("//*[@id='rowRel']/div[4]/div[2]"));
		Assert.assertEquals("Title", title.getText());
		WebElement actions = driver.findElement(By.xpath("//*[@id='rowRel']/div[4]/div[3]"));
		Assert.assertEquals("Actions", actions.getText());
		
	}
	
	@Test
	public void testBrowseTabUiElementsUserLoggedIn() throws Exception {
		
		Utils.Login(driver);
		Utils.waitForElement(driver, By.linkText("Browse Music"));
		Thread.sleep(3000);
		driver.findElement(By.linkText("Browse Music")).click();
//		WebElement browse_tab = driver.findElement(By.className("navigation"));
//		rowse_tab.findElement(By.linkText("Browse Music"));
				//linkText("Browse Music"));
//		Assert.assertEquals("Browse Music", browse_tab.getText());	
					
//		Thread.sleep(1000);
		System.out.println("TestBrowse>testBrowseTabUiElementsUserLoggedIn");
		
		Utils.waitForElement(driver, By.id("labels_box"));
		Utils.isElementPresent(driver, By.id("labels_box"));
		Utils.isElementPresent(driver, By.id("expand_labels"));
		WebElement libraries = driver.findElement(By.linkText("Label"));
		Assert.assertEquals("Label", libraries.getText());	
		WebElement projects = driver.findElement(By.linkText("Project Explorer"));
		Assert.assertEquals("Project Explorer", projects.getText());	
		
		WebElement album_view = driver.findElement(By.id("btn_trackview"));
		Assert.assertEquals(true, album_view.isEnabled());	
		Utils.isElementPresent(driver, By.id("btn_cdview"));
		Utils.isElementPresent(driver, By.id("sort"));
		Utils.isElementPresent(driver, By.id("results_num"));
		Utils.isElementPresent(driver, By.id("browse_list_acc"));
	
		WebElement list_view = driver.findElement(By.id("btn_cdview"));
		Assert.assertEquals("List View", list_view.getText());	
		list_view.click();
		Utils.waitForElement(driver, By.xpath("//*[@id='rowRel']/div[4]/div[1]"));
		//check header
		WebElement album_no = driver.findElement(By.xpath("//*[@id='rowRel']/div[4]/div[1]"));
		Assert.assertEquals("CD #", album_no.getText());
		WebElement description = driver.findElement(By.xpath("//*[@id='rowRel']/div[4]/div[2]"));
		Assert.assertEquals("Title", description.getText());
		WebElement actions = driver.findElement(By.xpath("//*[@id='rowRel']/div[4]/div[3]"));
		Assert.assertEquals("Actions", actions.getText());
			
	}
	
	@Test
	public void testBrowseSectionLibrariesTab() throws Exception {
		
		Utils.Login(driver);
		Utils.waitForElement(driver, By.linkText("Browse Music"));
		Thread.sleep(3000);
		driver.findElement(By.linkText("Browse Music")).click();
		
		Utils.waitForElement(driver, By.id("labels_box"));
		Utils.isElementPresent(driver, By.linkText("Label"));
		Utils.isElementPresent(driver, By.linkText("Browse all labels"));
		
		WebElement expand = driver.findElement(By.id("expand_labels"));
		Assert.assertEquals("Expand All", expand.getText());	
		expand.click();	
		Utils.waitForElement(driver, By.id("collapse_labels"));
		WebElement collapse = driver.findElement(By.id("collapse_labels"));
		Assert.assertEquals("Collapse All", collapse.getText());	
		collapse.click();	
			
	}
	
	@Test
	public void testBrowseSectionPlaylistsTab() throws Exception {
		
		Utils.waitForElement(driver, By.linkText("Browse Music"));
		Thread.sleep(3000);
		driver.findElement(By.linkText("Browse Music")).click();
		System.out.println("TestBrowse>testBrowseSectionPlaylistsTab");
		
		
		Utils.waitForElement(driver, By.id("labels_box"));
		WebElement playlists = driver.findElement(By.linkText("Playlists"));
		playlists.click();
		Utils.waitForElement(driver, By.linkText("** Top 20 Chart Sounds"));
		driver.findElement(By.linkText("** Top 20 Chart Sounds")).click();
		
		
		//check all actions
		Utils.isElementPresent(driver, By.linkText("Add to current project"));
		Utils.isElementPresent(driver, By.linkText("Print track list"));
		Utils.isElementPresent(driver, By.linkText("Download entire playlist"));
		Utils.isElementPresent(driver, By.linkText("Download entire playlist all edits"));
		//track list header
		WebElement cd_number = driver.findElement(By.xpath("/html/body/div[7]/div/div/div[3]/div/div/div/div/div/div[2]/div[2]/div/div[4]/div"));
		Assert.assertEquals("CD #", cd_number.getText());
		WebElement track_number = driver.findElement(By.xpath("/html/body/div[7]/div/div/div[3]/div/div/div/div/div/div[2]/div[2]/div/div[4]/div[2]"));
		Assert.assertEquals( "#", track_number.getText());
		WebElement track_title = driver.findElement(By.xpath("/html/body/div[7]/div/div/div[3]/div/div/div/div/div/div[2]/div[2]/div/div[4]/div[3]"));
		Assert.assertEquals("Title", track_title.getText());
		WebElement track_desc = driver.findElement(By.xpath("/html/body/div[7]/div/div/div[3]/div/div/div/div/div/div[2]/div[2]/div/div[4]/div[4]"));
		Assert.assertEquals("Description", track_desc.getText());
		WebElement track_lentgh = driver.findElement(By.xpath("/html/body/div[7]/div/div/div[3]/div/div/div/div/div/div[2]/div[2]/div/div[4]/div[5]"));
		Assert.assertEquals("Version", track_lentgh.getText());
		WebElement comp = driver.findElement(By.xpath("/html/body/div[7]/div/div/div[3]/div/div/div/div/div/div[2]/div[2]/div/div[4]/div[6]"));
		Assert.assertEquals("Composers", comp.getText());
		WebElement publisher = driver.findElement(By.xpath("/html/body/div[7]/div/div/div[3]/div/div/div/div/div/div[2]/div[2]/div/div[4]/div[7]"));
		Assert.assertEquals("Publishers", publisher.getText());
		WebElement actions = driver.findElement(By.xpath("/html/body/div[7]/div/div/div[3]/div/div/div/div/div/div[2]/div[2]/div/div[4]/div[8]"));
		Assert.assertEquals("Actions", actions.getText());

	}	
	
	@Test
	public void testBrowseTabDiscDetails() throws Exception {
		System.out.println("TestBrowse>testBrowseTabDiscDetails");
		Utils.waitForElement(driver, By.linkText("Browse Music"));
		Thread.sleep(3000);
		driver.findElement(By.linkText("Browse Music")).click();
						
		WebElement first_disc = driver.findElement(By.xpath("//*[@id='browse_list_acc']/div[1]/div[1]/a/img"));
		first_disc.click();
		
		Utils.waitForElement(driver, By.linkText("Add entire disc to project"));
		Utils.isElementPresent(driver, By.id("browseBackBtn"));
		//check title and  description
		Utils.isElementPresent(driver, By.className("cdtitle"));
		Utils.isElementPresent(driver, By.className("cdref"));
		//check right links 
		Utils.isElementPresent(driver, By.linkText("Add entire disc to project"));
		Utils.isElementPresent(driver, By.linkText("Print track list"));
		Utils.isElementPresent(driver, By.linkText("Download entire disc"));
		Utils.isElementPresent(driver, By.linkText("Download entire disc all edits"));
		//to do : check social media links
		//...
		//check that there is at least 1 track displayed
		Utils.isElementPresent(driver, By.xpath("//*[@id='browse_accordion']/div/div[1]/div[1]/div/a"));
			
	}	
	
	//                     ACTIONS TESTS
	
	
	@Test
	public void testBrowseAddDiscToProjectFromCDView() throws Exception {
		System.out.println("TestBrowse>testBrowseAddDiscToProjectFromCDView");
		Utils.Login(driver);
		Utils.waitForElement(driver, By.linkText("Browse Music"));
		Thread.sleep(3000);
		driver.findElement(By.linkText("Browse Music")).click();
		
		WebElement quick_add = driver.findElement(By.xpath("//*[@id='browse_list_acc']/div[1]/div[1]/p/a"));
		quick_add.click();
		Utils.waitForElement(driver, By.id("error_overlay"));
		WebElement close_warning = driver.findElement(By.xpath("//*[@id='error_overlay']/a"));
		close_warning.click();
		
		WebElement first_disc = driver.findElement(By.xpath("//*[@id='browse_list_acc']/div[1]/div[1]/a/img"));
		first_disc.click();
		String discName= first_disc.getAttribute("alt");
		System.out.println("Name of the added to project disc is " +discName);
			
		
		
		Utils.waitForElement(driver, By.linkText("Add entire disc to project"));
		WebElement add_link = driver.findElement(By.linkText("Add entire disc to project"));
		add_link.click();
		
		WebElement discExistIntoProjects = driver.findElement(By.cssSelector("div.row.overlayErrorContent"));
		if(discExistIntoProjects.isDisplayed())
		{
			JavascriptExecutor js =  (JavascriptExecutor) driver;
			js.executeScript("$('a.close').first().click();");
			//driver.findElements(By.cssSelector("div.row.overlayErrorContent a.close")).get(0).click();
		}
		
	   	WebElement expand_proj = driver.findElement(By.xpath("//*[@id='exp1']/div[1]/div[1]/div/div[2]/a/span/span[1]"));
	   	expand_proj.click();
	   	Thread.sleep(3000L);
	   	add_link.click();
	   	WebElement added_to_proj = driver.findElement(By.xpath("//*[@id='exp1']/div[1]/div[2]/div[2]/div[1]/div/div[2]/a/span/span[2]"));
	   	
	//  	WebElement discExistIntoProjects = driver.findElement(By.cssSelector("div.row.overlayErrorContent"));
		if(discExistIntoProjects.isDisplayed()) 
		{
			JavascriptExecutor js =  (JavascriptExecutor) driver;
			js.executeScript("$('a.close').first().click();");
			//driver.findElements(By.cssSelector("div.row.overlayErrorContent a.close")).get(0).click();
		}
		
//	   	WebElement cd_title = driver.findElement(By.xpath("//*[@id='outline']/div/div[3]/div[1]/div/div[2]/div/div[2]/div/div[2]/div/div[1]/div[2]/span[1]"));
//	   	Assert.assertEquals(true, added_to_proj.getText().contains(cd_title.getText()));
	   	driver.navigate().refresh(); Thread.sleep(1000);
	   	//expand_proj.click();
	   	List<WebElement> subitems = driver.findElements(By.cssSelector("div.project_itm a.acc2"));
	   	for(int i=0; i<subitems.size(); i++)
	   	{
	   		String text = driver.findElements(By.cssSelector("div.project_itm a.acc2 span.title")).get(i).getText();
	   		System.out.println(text);
	   		if(!(i==0) &&  text.equals(discName) ){
	   			driver.findElements(By.cssSelector("div.rel a.deletebtn")).get(i+1).click();
	   		}
	   	}
			
	}
	
	@Test
	public void testBrowseAddDiscToProjectFromListView() throws Exception {
		try
		{
			System.out.println("testBrowseAddDiscToProjectFromListView");
			Utils.Login(driver);
			Utils.waitForElement(driver, By.linkText("Browse Music"));
			Thread.sleep(3000);
			driver.findElement(By.linkText("Browse Music")).click();
						
					
			WebElement list_view = driver.findElement(By.id("btn_cdview"));
			list_view.click();
			
			Utils.waitForElement(driver, By.xpath("//*[@id='rowRel']/div[4]/div[1]"));
			driver.findElement(By.xpath(".//*[@id='pageRows']/div[1]/div[1]/div/div/div/div[1]/div[1]/div[2]/a")).click();
			driver.findElement(By.xpath(".//*[@id='exp1']/div[1]/div[1]/div/div[2]/a/span/span[1]")).click();
			driver.findElement(By.xpath("/html/body/div[7]/div/div/div[3]/div/div/div/div/div/div[2]/div[3]/div/div/div/a/span/img")).click();
			Thread.sleep(1000);
			
//			driver.findElement(By.cssSelector("a.exp1.hovered > span.item_header > span.button")).click();
					    
			driver.findElement(By.xpath(".//*[@id='ui-accordion-browse_list_acc-header-0']/div/div[1]/a[2]")).click();
//			driver.findElement(By.xpath(".//*[@id='ui-accordion-browse_list_acc-header-0']/div/div[1]/a[2]")).click();
			
			 
			
			
			//go to first cd
			//WebElement first_CD = driver.findElement(By.xpath("//*[@id='browse_list_acc']/div[1]/div[1]/div/a/span[1]/img"));
			//hidden btns	
//			JavascriptExecutor js= (JavascriptExecutor) driver;

//			js.executeScript("jQuery('div.rel a.main.ui-draggable').first().click()");
//			js.executeScript("jQuery('.dwnload').click()");
//			WebElement first_disc = driver.findElements(By.cssSelector("div.rel a.main.ui-draggable")).get(1);
//			first_disc.click(); 
//			 driver.findElement(By.cssSelector("a.main.ui-draggable .cdv_thumb")).click();
			//check that the cd was added
			 
//			driver.findElement(By.cssSelector("a.tabtitle")).click();
//		   	WebElement expandProj = driver.findElement(By.cssSelector("a.exp1"));
//		   	expandProj.click();
//		   	Thread.sleep(5000);
	
//		   	Thread.sleep(3000L);
//		   	WebElement added_to_proj = driver.findElement(By.xpath("//*[@id='exp1']/div[1]/div[2]/div[2]/div[1]/div/div[2]/a/span/span[2]"));
//		   	WebElement cd_title = driver.findElement(By.xpath("//*[@id='browse_list_acc']/div[1]/div[1]/div/a/span[2]/span[1]"));
//		   	WebElement cdNumber = driver.findElements(By.cssSelector(".cdv_title_disc_ref span.cdv_cdref")).get(0);
	//	   	Assert.assertEquals(added_to_proj.getText().trim(), cd_title.getText() +" - "+ cdNumber.getText().trim());
		}
		catch(Exception e)
		{
			System.out.println("TestBrowse>testBrowseAddDiscToProjectFromListView");
			System.out.println(e.getMessage());
		}
			
	}
	
	@After
	public void tearDown() throws Exception {
		driver.quit();
	}
	
}
